path = './test_data.txt'
path2 = './out.txt'
path3 = './test_data_matlab.txt'
path4 = './out_matlab.txt'
# 只读模式打开文件
fid1 = open(path, 'r', encoding='utf-8')
fid2 = open(path2, 'r', encoding='utf-8')

fid3 = open(path3, 'w+', encoding='utf-8')
fid4 = open(path4, 'w+', encoding='utf-8')

fid1_data = fid1.read()
fid1_list = fid1_data.split()
for i in range(len(fid1_list)):
    # print(i, ':', fid1_list[i])
    # temp = "0x" + fid1_list[i]
    c = bin(int(fid1_list[i], 16))
    c1 = c[2:]
    c2 = (8 - len(c1)) * "0" + str(c1)
    fid3.write(c2)

fid2_data = fid2.read()
fid2_list = fid2_data.split()
for i in range(len(fid2_list)):
    c = bin(int(fid2_list[i], 16))
    c1 = c[2:]
    c2 = (8 - len(c1)) * "0" + str(c1)
    fid4.write(c2)

fid1.close()
fid2.close()
fid3.close()
fid4.close()
